﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MemberServerBLL;

public partial class ControlPanel_Member_UserRegResult : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            try
            {

                if (!(bool)Session["UserExists"])
                {

                    //用户基本信息的Panel 
                    Panel UserBasePanel = (Panel)PreviousPage.Form.FindControl("ContentPlaceHolder1").FindControl("exBaseInfoPanel");
                    //用户扩展信息的Panel 
                    Panel UserExtendPanel = (Panel)PreviousPage.Form.FindControl("ContentPlaceHolder1").FindControl("DetailInfoPanel");

                    TextBox UserName = (TextBox)UserBasePanel.FindControl("UserName");
                    TextBox Password = (TextBox)UserBasePanel.FindControl("Password");
                    TextBox Email = (TextBox)UserBasePanel.FindControl("Email");
                    TextBox Question = (TextBox)UserBasePanel.FindControl("Question");
                    TextBox Answer = (TextBox)UserBasePanel.FindControl("Answer");
                    //从配置文件读取，判断用户是否需要验证
                    bool isApproved = !Convert.ToBoolean(ConfigurationManager.AppSettings["NeedApproved"]);

                    //注册用户普通信息
                    MembershipCreateStatus status;
                    MembershipUser newUser = Membership.CreateUser(UserName.Text.Trim(), Password.Text.Trim(), Email.Text.Trim(), Question.Text.Trim(), Answer.Text.Trim(), isApproved, out status);


                    //注册用户扩展信息
                    if (status == MembershipCreateStatus.Success)
                    {
                        #region 获得控件并取得值
                        TextBox txt_NickName = (TextBox)UserExtendPanel.FindControl("txt_NickName");
                        TextBox txt_TrueName = (TextBox)UserExtendPanel.FindControl("txt_TrueName");
                        RadioButtonList rbl_Sex = (RadioButtonList)UserExtendPanel.FindControl("rbl_Sex");
                        TextBox txt_BirthDay = (TextBox)UserExtendPanel.FindControl("txt_BirthDay");
                        TextBox txt_IdCard = (TextBox)UserExtendPanel.FindControl("txt_IdCard");
                        TextBox txt_Country = (TextBox)UserExtendPanel.FindControl("txt_Country");
                        TextBox txt_Province = (TextBox)UserExtendPanel.FindControl("txt_Province");
                        TextBox txt_City = (TextBox)UserExtendPanel.FindControl("txt_City");
                        TextBox txt_Address = (TextBox)UserExtendPanel.FindControl("txt_Address");
                        TextBox txt_Postcode = (TextBox)UserExtendPanel.FindControl("txt_Postcode");
                        TextBox txt_Profession = (TextBox)UserExtendPanel.FindControl("txt_Profession");
                        TextBox txt_Sign = (TextBox)UserExtendPanel.FindControl("txt_Sign");
                        CheckBox chk_EnableSign = (CheckBox)UserExtendPanel.FindControl("chk_EnableSign");
                        TextBox txt_Blog = (TextBox)UserExtendPanel.FindControl("txt_Blog");
                        TextBox txt_Interest = (TextBox)UserExtendPanel.FindControl("txt_Interest");
                        CheckBox chk_EnableImage = (CheckBox)UserExtendPanel.FindControl("chk_EnableImage");
                        TextBox txt_PrivateEmail = (TextBox)UserExtendPanel.FindControl("txt_PrivateEmail");
                        CheckBox chk_EnableReviceEmail = (CheckBox)UserExtendPanel.FindControl("chk_EnableReviceEmail");
                        TextBox txt_QQ = (TextBox)UserExtendPanel.FindControl("txt_QQ");
                        TextBox txt_MobilePhone = (TextBox)UserExtendPanel.FindControl("txt_MobilePhone");
                        TextBox txt_TelePhone = (TextBox)UserExtendPanel.FindControl("txt_TelePhone");
                        TextBox txt_Fax = (TextBox)UserExtendPanel.FindControl("txt_Fax");
                        TextBox txt_Dep = (TextBox)UserExtendPanel.FindControl("txt_Dep");


                        //从控件中获得值
                        System.Guid UserId = new Guid(newUser.ProviderUserKey.ToString());
                        //用户号生成：目前作为随机数
                        int UserNo = new Random().Next(100000);
                        string NickName = txt_NickName.Text.Trim();
                        string TrueName = txt_TrueName.Text.Trim();
                        bool? Sex = null;
                        if (rbl_Sex.SelectedValue != "")
                            Sex = Convert.ToBoolean(rbl_Sex.SelectedValue);
                        System.DateTime? BirthDay = null;
                        if (txt_BirthDay.Text.Trim() != "")
                            BirthDay = Convert.ToDateTime(txt_BirthDay.Text.Trim());
                        else
                        {
                            //如果用户的生日没有填写，那就默认为1900-01-01
                            BirthDay = Convert.ToDateTime("1900-01-01");
                        }
                        string IdCard = txt_IdCard.Text.Trim();
                        string Country = txt_Country.Text.Trim();
                        string Province = txt_Province.Text.Trim();
                        string City = txt_City.Text.Trim();
                        string Address = txt_Address.Text.Trim();
                        string Postcode = txt_Postcode.Text.Trim();
                        string Profession = txt_Profession.Text.Trim();
                        //刚刚注册，账户没有钱
                        decimal Account = 0;
                        string Signature = txt_Sign.Text.Trim();
                        bool EnableSign = chk_EnableSign.Checked;
                        string Blog = txt_Blog.Text.Trim();
                        string Interest = txt_Interest.Text.Trim();
                        byte[] Images = (byte[])Session["ImageData"];
                        string ImageType = Session["ImageType"].ToString();
                        bool EnableImage = chk_EnableImage.Checked;
                        string PrivateEmail = txt_PrivateEmail.Text.Trim();
                        bool EnableReviceEmail = chk_EnableReviceEmail.Checked;
                        string QQ = txt_QQ.Text.Trim();
                        string MobilePhone = txt_MobilePhone.Text.Trim();
                        string Telephone = txt_TelePhone.Text.Trim();
                        string Fax = txt_Fax.Text.Trim();
                        string Department = txt_Dep.Text.Trim();
                        #endregion

                        //保存扩展资料（详细信息和联系方式）
                        BLL_UserDetail userDetailBll = new BLL_UserDetail();
                        if (userDetailBll.UpdateUserDetail(UserId, UserNo,//用户编码功能，等第下一个版本实现
                            NickName, TrueName, Sex,
                            BirthDay,
                            IdCard,
                            Country, Province, City, Address, Postcode,
                            Profession,
                            Account,
                            Signature, EnableSign,
                            Blog,
                            Interest,
                            Images, ImageType, EnableImage,
                            PrivateEmail, EnableReviceEmail,
                            QQ, Telephone, MobilePhone, Fax, Department) != 1)
                        {
                            //如果保存失败，要删除数据
                            Membership.DeleteUser(UserName.Text, true);

                            lbl_RegResult.Text = "<font color=red>保存用户信息失败,请检查输入是否有误!</font>";
                        }
                        else
                        {
                            lbl_RegResult.Text = "<font color=red>注册成功!!，谢谢您对本站的支持</font>";
                            lbl_Username.Text = "您的用户名是：" + UserName.Text;
                            lbl_UserNo.Text = "您的客户号是：" + UserNo;
                        }
                    }
                }
                else
                {
                    lbl_RegResult.Text = "<font color=red>保存用户信息失败,请检查输入是否有误!</font>";
                }

            }
            catch (Exception)
            {

                throw;
            }


        }
        else
        {
            Response.Write("请不要直接访问本页");
            Response.End();
        }
    }

}
